package controllers

import (
	"github.com/astaxie/beego"
	"github.com/astaxie/beego/orm"
	"nspel/entity"
	"nspel/model"
	"nspel/nspel"
)

type CityController struct {
	beego.Controller
}

func (c *CityController) GetCitySelect() {
	provinceId, err := c.GetInt64("province-id")
	city := new(entity.City)
	var cities *[]entity.City
	if err == nil {
		cities, err = city.SelectAllByProvinceId(provinceId)
	} else {
		cities, err = city.SelectAll()
	}
	if err != nil {
		if err == orm.ErrNoRows {
			c.Data["json"] = model.GetCitySelectRes{}
		} else {
			c.Data["json"] = model.ErrorRes{RetCode: 2, Msg: "服务器运行错误"}
			nspel.Error(err)
		}
	} else {
		res := model.GetCitySelectRes{RetCode: 0}
		res.City = make([]model.CitySelect, len(*cities))
		for i := 0; i < len(res.City); i++ {
			res.City[i].Id = (*cities)[i].Id
			res.City[i].Value = (*cities)[i].Name
		}
		c.Data["json"] = res
	}
	c.ServeJSON()
}
